/**
 * 
 */
package br.financeiro.adapters;

import java.util.ArrayList;

import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import br.financeiro.R;

/**
 * @author evaldosavio
 * 
 */
public class SimpleExpandableAdapter extends BaseExpandableListAdapter {
	private LayoutInflater inflater;
	private ArrayList<ItemListView> itens;

	public SimpleExpandableAdapter( Context context, ArrayList<ItemListView> itens ) {
		this.itens = itens;
		this.inflater = LayoutInflater.from( context );
	}

	public SimpleExpandableAdapter() {
		itens = new ArrayList<ItemListView>();
	}

	@Override
	public Object getChild( int groupPosition, int childPosition ) {
		return itens.get( groupPosition ).getSubItens().get( childPosition );
	}

	@Override
	public long getChildId( int groupPosition, int childPosition ) {
		return childPosition;
	}

	@Override
	public int getChildrenCount( int groupPosition ) {
		return itens.get( groupPosition ).getSubItens().size();
	}

	@Override
	public Object getGroup( int groupPosition ) {
		return itens.get( groupPosition );
	}

	@Override
	public int getGroupCount() {
		return itens.size();
	}

	@Override
	public long getGroupId( int groupPosition ) {
		return groupPosition;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable( int groupPosition, int childPosition ) {
		return true;
	}

	@Override
	public View getGroupView( int groupPosition, boolean isExpanded, View view, ViewGroup viewGroup ) {
		if ( view == null ) {
			view = inflater.inflate( R.layout.person_item_layout, viewGroup, false );
		}
		TextView name = (TextView) view.findViewById( R.id.labelPersonName );
		name.setText( itens.get( groupPosition ).getTitle() );
		return view;
	}

	@Override
	public View getChildView( int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup viewGroup ) {
		if ( view == null )
			view = inflater.inflate( R.layout.person_sub_item, viewGroup, false );
		TextView text = (TextView) view.findViewById( R.id.text );
		text.setText( itens.get( groupPosition ).getSubItens().get( childPosition ) );

		ImageView icon = (ImageView) view.findViewById( R.id.icon );
		icon.setImageResource( itens.get( groupPosition ).getIconsItens().get( childPosition ) );

		return view;
	}

	@Override
	public void registerDataSetObserver( DataSetObserver observer ) {
		super.registerDataSetObserver( observer );
	}
}
